<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="排序" prop="sort">
        <el-input v-model="queryParams.sort" placeholder="请输入排序" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="创建人" prop="createUser">
        <el-input v-model="queryParams.createUser" placeholder="请输入创建人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="queryParams.updateUser" placeholder="请输入修改人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否删除 1-是 0-否" prop="isDel">
        <el-input v-model="queryParams.isDel" placeholder="请输入是否删除 1-是 0-否" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="父类线路id，顶级默认为0" prop="parentId">
        <el-input v-model="queryParams.parentId" placeholder="请输入父类线路id，顶级默认为0" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="级别：1：一级；2：二级；3：三级" prop="level">
        <el-input v-model="queryParams.level" placeholder="请输入级别：1：一级；2：二级；3：三级" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="线路名称" prop="lineName">
        <el-input v-model="queryParams.lineName" placeholder="请输入线路名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="线路编号" prop="lineCode">
        <el-input v-model="queryParams.lineCode" placeholder="请输入线路编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:maintenance:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:maintenance:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:maintenance:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:maintenance:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="table1" v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" width="80" align="center" prop="id" />
      <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
      <!-- <el-table-column label="创建人" align="center" prop="createUser" /> -->
      <!-- <el-table-column label="修改人" align="center" prop="updateUser" /> -->
      <!-- <el-table-column label="是否删除 1-是 0-否" align="center" prop="isDel" /> -->
      <!-- <el-table-column label="父类线路id，顶级默认为0" align="center" prop="parentId" /> -->
      <!-- <el-table-column label="父线路id" align="center" prop="parentId" />
      <el-table-column label="级别" align="center" prop="level" /> -->
      <el-table-column label="线路名称" align="center" prop="lineName" />
      <el-table-column label="线路编号" align="center" prop="lineCode" />
      <el-table-column label="线路类型" align="center">
        <template slot-scope="scope">
          <span class="str" v-for="(option, index) in idcList" :key="index"
            v-if="scope.row.lineType == option.dictValue">{{ option.dictLabel }}</span>
        </template>
      </el-table-column>
      <el-table-column type="expand" width="1">
        <template slot-scope="scope">
          <el-table ref="table2" :data="scope.row.secondData" :show-header=false class="secondTable"
            v-if="scope.row.secondData.length > 0" v-loading="scope.row.loading">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column width="50" align="center"> </el-table-column>
            <el-table-column label="主键ID" width="80" align="center" prop="id" />
            <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
            <el-table-column label="父线路id" align="center" prop="parentId" />
            <el-table-column label="级别" align="center" prop="level" />
            <el-table-column label="线路名称" align="center" prop="lineName" />
            <el-table-column label="线路编号" align="center" prop="lineCode" />
            <el-table-column label="线路类型" align="center">
              <template slot-scope="second">
                <span class="str" v-for="(option, index) in idcList" :key="index"
                  v-if="second.row.lineType == option.dictValue">{{ option.dictLabel }}</span>
              </template>
            </el-table-column>
            <el-table-column type="expand" width="1">
              <template slot-scope="second">
                <el-table ref="table3" :data="second.row.secondData" :show-header=false class="secondTable"
                  v-loading="second.row.loading">
                  <!-- <el-table-column type="selection" width="55" align="center" /> -->
                  <el-table-column width="50" align="center"> </el-table-column>
                  <el-table-column label="主键ID" width="80" align="center" prop="id" />
                  <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
                  <!-- <el-table-column label="父线路id" align="center" prop="parentId" />
                  <el-table-column label="级别" align="center" prop="level" /> -->
                  <el-table-column label="线路名称" align="center" prop="lineName" />
                  <el-table-column label="线路编号" align="center" prop="lineCode" />
                  <el-table-column label="线路类型" align="center">
                    <template slot-scope="third">
                      <span class="str" v-for="(option, index) in idcList" :key="index"
                        v-if="third.row.lineType == option.dictValue">{{ option.dictLabel }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="third">
                      <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(third.row)"
                        v-hasPermi="['system:maintenance:edit']">修改</el-button>
                      <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(third.row)"
                        v-hasPermi="['system:maintenance:remove']">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="second">
                <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAddRank(second.row, 2)"
                  v-hasPermi="['system:maintenance:edit']">新增三级</el-button>
                <el-button size="mini" type="text" icon="el-icon-search" @click="handleShow(second.row, 2)"
                  v-hasPermi="['system:maintenance:edit']">查看三级</el-button>
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(second.row)"
                  v-hasPermi="['system:maintenance:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(second.row)"
                  v-hasPermi="['system:maintenance:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAddRank(scope.row, 1)"
            v-hasPermi="['system:maintenance:edit']">新增二级</el-button>
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleShow(scope.row, 1)"
            v-hasPermi="['system:maintenance:edit']">查看二级</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:maintenance:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:maintenance:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改线路维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="form.sort" type="number" placeholder="请输入排序" />
        </el-form-item>
        <!-- <el-form-item label="父线路id" prop="parentId">
          <el-input v-model="form.parentId" disabled placeholder="请输入父类线路id，顶级默认为0" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select clearable v-model="form.level" disabled placeholder="请选择级别">
            <el-option :value="1" label="一级" />
            <el-option :value="2" label="二级" />
            <el-option :value="3" label="三级" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="线路名称" prop="lineName">
          <el-input v-model="form.lineName" placeholder="请输入线路名称" />
        </el-form-item>
        <el-form-item label="线路编号" prop="lineCode">
          <el-input v-model="form.lineCode" placeholder="请输入线路编号" />
        </el-form-item>
        <el-form-item label="线路类型" prop="lineType">
          <el-select clearable v-model="form.lineType" placeholder="请选择线路类型">
            <el-option v-for="(option, index) in idcList" :key="index" :value="option.dictValue"
              :label="option.dictLabel" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaintenance, getMaintenance, delMaintenance, addMaintenance, updateMaintenance } from "@/api/system/maintenance";
import { getDicts as getDicts } from '@/api/system/dict/data'
export default {
  name: "Maintenance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线路维护表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: null,
        createUser: null,
        updateUser: null,
        isDel: null,
        parentId: 0,
        level: 1,
        lineName: null,
        lineCode: null,
        lineType: null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        isDel: [
          { required: true, message: "是否删除 1-是 0-否不能为空", trigger: "blur" }
        ],
      },
      idcList: [],
    };
  },
  created() {
    this.getList();
    getDicts('line_type').then(res => {
      if (res.code == 200) {
        this.idcList = res.data || [];
      }
    })
  },
  methods: {
    /** 查询线路维护列表 */
    getList() {
      this.loading = true;
      listMaintenance(this.queryParams).then(response => {
        let maintenanceList = response.rows || [];
        for (let i in maintenanceList) {
          maintenanceList[i].loading = false;
          maintenanceList[i].secondData = [];
        }
        this.maintenanceList = maintenanceList;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sort: null,
        updateTime: null,
        createTime: null,
        createUser: null,
        updateUser: null,
        isDel: null,
        parentId: 0,
        level: 1,
        lineName: null,
        lineCode: null,
        lineType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        sort: null,
        createUser: null,
        updateUser: null,
        isDel: null,
        parentId: 0,
        level: 1,
        lineName: null,
        lineCode: null,
        lineType: null
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线路维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMaintenance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改线路维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除线路维护主键ID为"' + ids + '"的数据项？').then(function () {
        return delMaintenance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/maintenance/export', {
        ...this.queryParams
      }, `maintenance_${new Date().getTime()}.xlsx`)
    },
    handleExpandChange(row, expandedRows) {
      // console.log(row);
      // console.log(expandedRows);
      // // 这里可以执行你需要的操作
      // if (expandedRows.length > 0) {
      //   for (let m of this.maintenanceList) {
      //     if (m.id === row.id) {
      //       m.loading = true
      //       this.loading = true;
      //       let queryParams = this.queryParams;
      //       queryParams.pageSize = 100;
      //       queryParams.id = row.id;
      //       queryParams.level++;
      //       listMaintenance(queryParams).then(response => {
      //         let secondData = response.rows || [];
      //         this.loading = false;
      //         m.secondData = secondData;
      //       });
      //       setTimeout(() => { m.loading = false }, 500)
      //       break
      //     }
      //   }
      // }
    },
    handleShow(row, num) {
      let table = this.$refs['table' + num];
      table.toggleRowExpansion(row);
      for (let m of this.maintenanceList) {
        if (num == 1) {
          if (m.id === row.id) {
            m.loading = true
            this.loading = true;
            let queryParams = {
              pageNum: 1,
              pageSize: 100,
              id: row.id,
              level: row.level + 1
            }
            listMaintenance(queryParams).then(response => {
              let secondData = response.rows || [];
              secondData.forEach(obj => {
                obj.loading = false;
                obj.secondData = [];
              });
              this.loading = false;
              m.secondData = secondData;
              if (secondData.length == 0) {
                this.$modal.msg('当前无下级信息')
              }
            });
            setTimeout(() => { m.loading = false }, 500)
            break
          }
        } else if (num == 2) {
          if (m.id === row.parentId) {
            for (let n of m.secondData) {
              if (n.id == row.id) {
                n.loading = true
                this.loading = true;
                let queryParams = {
                  pageNum: 1,
                  pageSize: 100,
                  id: row.id,
                  level: row.level + 1
                }
                listMaintenance(queryParams).then(response => {
                  let secondData = response.rows || [];
                  this.loading = false;
                  n.secondData = secondData;
                  if (secondData.length == 0) {
                    this.$modal.msg('当前无下级信息')
                  }
                });
                setTimeout(() => { n.loading = false }, 500)
                break
              }
            }
          }
        }
      }
    },
    handleAddRank(row, num) {
      console.log(row)
      this.reset();
      this.form = {
        id: null,
        sort: null,
        updateTime: null,
        createTime: null,
        createUser: null,
        updateUser: null,
        isDel: null,
        parentId: row.id,
        level: row.level + 1,
        lineName: null,
        lineCode: null,
        lineType: null,
      };
      this.open = true;
      this.title = "添加线路维护";
    },
  }
};
</script>
<style>
.secondTable {
  padding-left: 15px;
}

.el-table__expand-icon {
  display: none;
}

.el-table__expanded-cell {
  padding: 0 !important;
  border: none !important;
}
</style>
